import 'dart:convert';

import 'package:get/get.dart';
import 'package:star_author/model/user_info_entity.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_keys_constant.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';

class UserGetXController extends GetxController {
  UserInfoEntity? _user;
  String? _token;

  void setUserInfo(UserInfoEntity user) {
    _user = user;
    update();
    updateSpInfo();
  }

  UserInfoEntity? getUserInfo() {
    return _user;
  }

  void setToken(String token) {
    _token = token;
    updateSpInfo();
  }

  String? getToken() {
    return _token;
  }

  //更新缓存
  void updateSpInfo() {
    Map _map = {
      "token": _token,
      "user": _user?.toJson(),
    };
    SpUtil.putString(SpKeysConstant.loginInfo, jsonEncode(_map));
  }
}
